package net.dlz.example.controller;

import net.dlz.example.domain.Announce;
import net.dlz.example.domain.SearchAnnounceResponse;
import net.dlz.example.service.AnnounceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/Announce")
public class AnnounceController {

    @Autowired
    private AnnounceService announceService;

    @RequestMapping("/list")
    public String AnnounceList(Model model){
        List<Announce> announces =announceService.listAnnounce();
        model.addAttribute("announces",announces);
        return "announce_list";}

    @RequestMapping("/alist")
    public String AnnounceAdminList(){return "announceadmin_list";}

    @GetMapping("/api/search")
    @ResponseBody
    public SearchAnnounceResponse searchAnnounceResponse(@RequestParam("pageNum") Integer pageNum,
                                                     @RequestParam("pageSize") Integer pageSize){
        return announceService.findAllAnnounce(pageNum,pageSize);
    }

    @GetMapping("/api/searchLink")
    @ResponseBody
    public SearchAnnounceResponse searchAnnounceResponseLink(@RequestParam("keywords") String keywords,
                                                     @RequestParam("pageNum") Integer pageNum,
                                                     @RequestParam("pageSize") Integer pageSize){
        return announceService.findAnnounceLinkName(keywords,pageNum,pageSize);
    }
    @RequestMapping("/toadd")
    public String addAnnouncePage(Model model){
        model.addAttribute("announce",new Announce());
        return "announce_submit";
    }
    @RequestMapping("/add")
    public String addAnnounce(Announce announce){
        int updated = announceService.saveAnnounce(announce);
        return "redirect:/Announce/list";
    }




    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deleteArea(@RequestParam("id") String id){
        int deleted = announceService.deleteAnnounce(id);
        return deleted > 0;
    }
    @RequestMapping("/content")
    public String PolicyContent(@RequestParam("id") String id,Model model){
        Announce announce = announceService.findAnnounceByPrimaryKey(id);
        model.addAttribute("announce",announce);
        return "announce_content";
    }

}

